﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Grids
{
    public partial class GridTransformerAsset : GridBase, IGridBase
    {
        //Delegates for KML and Google Earth
        private delegate void GetKmlDelegate();
        private GetKmlDelegate getKmlDelegate;

        public GridTransformerAsset()  : base(Common.Enumerations.DataTypes.AssetsTransformers)
        {
            InitializeComponent();
            advTreeAssets.Visible = false;
            ViewOnMapItemVisible = true;
            contextMenuBarGrid.SetContextMenuEx(advTreeAssets, buttonItemContextMenu);
        }

        #region IGridBase Members

        public override void DoAsynchLoad(DoWorkEventArgs e)
        {
            advTreeAssets.Invoke(new MethodInvoker(delegate { advTreeAssets.Enabled = false; }));

            int _TotalRecords;
            e.Result = ServiceProxy.UtilityService.GetTransformerAssetData(DataKeyId, PageSize, PageNumber, Common.Enumerations.GridSearchByTpes.None, null, out _TotalRecords);
            TotalItems = _TotalRecords;
        }

        public override void AsychLoadComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychLoadComplete(sender, e);
            //do whatever needs to be done after asynch work complete (BackgroudWorker_RunWorkerCompleted)
            if (e.Cancelled)
            {

            }

            if (e.Error != null || e.Result == null)
            {
                //todo: create some sort of error dialog and use it here instead of messagebx
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving project data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            var _data = e.Result as List<EntityLibrary.UtilityAssetTransformerEntity>;
            
            if (_data == null || _data.Count == 0)
            {
                if (CurrentSearchType == Common.Enumerations.GridSearchByTpes.None)
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the selected asset type.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the asset type with the applied search.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            bindingSourceGridDataParent.DataSource = _data;
            advTreeAssets.DataSource = bindingSourceGridDataParent;
            advTreeAssets.Bind();
            advTreeAssets.Visible = true;
            advTreeAssets.Enabled = true;
        }

        public override void DoAsynchSave(DoWorkEventArgs e)
        {
        }

        public override void AsychSaveComplete(object sender, RunWorkerCompletedEventArgs e)
        {
        }

        public override void DoAsynchGetKml(DoWorkEventArgs e)
        {
            advTreeAssets.Invoke(new MethodInvoker(delegate { advTreeAssets.Enabled = false; }));
            //Kml = ServiceProxy.UtilityService.GetTransformerAssetKML(DataKeyId, CurrentSearchType, CurrentSearchText);

            if (getKmlDelegate != null)
                getKmlDelegate();
        }

        public override void AsychGetKmlComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychGetKmlComplete(sender, e);
            advTreeAssets.Enabled = true;

            if (e.Cancelled)
            {
                // not sure what to do
            }

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving map data.",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ShowKmlInMap();
        }




        
        public override void DoSearch(Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {
        }

        public override void DoExportToExcel()
        {
            //just call method in base
            base.DoExportToExcel();
        }

        public override void DoEdit()
        {
        }

        public override void DoOpen()
        {
        }

        public void GetTransformersAssetKML()
        {
            Kml = ServiceProxy.UtilityService.GetTransformerAssetKML(DataKeyId, CurrentSearchType, CurrentSearchText);
        }

        public void GetTransformerItemAssetKML()
        {
            var selectedTransformerItem = advTreeAssets.GetSelectedEntity() as EntityLibrary.UtilityAssetTransformerEntity;

            if (selectedTransformerItem != null)
                Kml = ServiceProxy.UtilityService.GetDistributionTransformersAssetKmlString(selectedTransformerItem.Utility_Asset_TransformerId.ToString());
        }

        public void ShowData(int utilityId)
        {
            DataKeyId = utilityId;
            advTreeAssets.Visible = false;
            DataDescription = "Distribution Assets";
            StartAsynchLoad();
        }

        public override bool ContextShowAllowed()
        {
            var _Transformer  = advTreeAssets.GetSelectedEntity() as EntityLibrary.UtilityAssetTransformerEntity;

            if (_Transformer == null)
                return false;

            SelectedRowLatitude = _Transformer.Latitude;
            SelectedRowLongitude = _Transformer.Longitude;

            return true;
        }

        public override void DoMapForGrid()
        {
            getKmlDelegate = GetTransformersAssetKML;
            StartAsynchGetKml();
        }

        public override void DoMapForRecord()
        {
            getKmlDelegate = GetTransformerItemAssetKML;
            StartAsynchGetKml();
        }

        #endregion
    }
}
